<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
<meta content="yes" name="apple-mobile-web-app-capable">
<meta content="black" name="apple-mobile-web-app-status-bar-style">
<meta content="telephone=no" name="format-detection">
<link rel="stylesheet" type="text/css" href="../static/easyui/themes/bootstrap/easyui.css">
<link rel="stylesheet" type="text/css" href="../static/easyui/themes/icon.css">
<link rel="stylesheet" href="../static/css/style-app.css" />
<script src="../static/jquery/jquery-1.12.4.js"></script>
<script src="../static/easyui/jquery.easyui.min.js"></script>
<script src="../static/easyui/locale/easyui-lang-zh_CN.js"></script>
<title>日计划</title>
</head>
<style>
.search-input.clear{height:30px;line-height: 30px;}
</style>
<body>
	<div class="top-part">
		<div class="header">
			<div class="return">
				<a href="javascript:history.go(-1);"></a>
			</div>
			<div class="header-title">日计划列表</div>
		</div>

	</div>
	<div class="table-container" data-options="region:'center'">
		<div>
			<div id="pro_tb">
				<div class="search-input clear">
					<span class="fl">发生日期：</span>
					<span class="dates">
						<span><input id="fromDate" class="easyui-datebox" style="width: 100%;" /></span>
						<span>~</span>
						<span><input id="toDate" class="easyui-datebox" style="width: 100%;" /></span>
					</span>
				</div>
				<div class="search-input text-right">
					<a href="#" class="d-btn btn-yellow" id="search">搜索</a>
					<a href="javascript:void(0)" class="d-btn btn-blue" onclick="newpro()">添加</a>
				</div>
			</div>
			<table id="pro_table" class="easyui-datagrid" style="min-height: 100%">
			</table>
		</div>
	</div>

</body>
<script th:inline="javascript">
	var currentPro;
	var fromDate = ""; 
	var toDate = ""; 
	var planmain =[[${planmain}]];
	var planmainid= planmain.id;
	$(document).ready(function() {
		$('#pro_table').datagrid({
			rownumbers : true,
			singleSelect : true,
			pagination : true,
			fitColumns : false,
			border : false,
			striped : true,
			toolbar : '#pro_tb',
			url : "../plandaily",
			method : 'get',
			queryParams: {
				planmainid : planmainid,
				startdate : fromDate,
				enddate : toDate
			},
			onSelect : function(index, row) {
				currentPro = row;
			},
			columns : [ [ {
				field : 'content',
				title : '内容',
				width : '100%',
				formatter : function(value,row,index) {
					var html = '';
					html += '<div class="info-unreviewed">'
					html +='<div class="info-item-content" style="width:'+ ellipsis(60) +'px;margin-bottom:10px;">'+ row.content+'</div>'
					html += '<div class="info-time">'+ formatDate(row.plandate) +'</div>'
					if(row.report){
						html+='<div class="info-money green">'+ row.percent * row.report.percent / 100  +'%<span class="info-unit">/'+ row.percent +'%</span></div>';

					}
					html += '<div class="edit">'
					if (row.status == 4){
						html += '<div class="info-status ok">已审</div><span class="info-edit" onclick="updatepro('+index+')"></span>';
					}
					if (row.status == 3){
						html += '<div class="info-reason">'+ row.reason +'</div><div class="info-status refuse">已拒绝</div><span class="info-edit" onclick="updatepro('+index+')"></span>';
					}
					if (row.status == 6){
						html += '<div class="info-status two">待二级审核</div><span class="info-edit" onclick="updatepro('+index+')"></span><span class="info-delete" onclick="deletepro('+index+')"></span>';
					}
					html += '</div></div>';
					return html;
				}
			} ] ]
		});
	});
	function ellipsis(value){
		var WIDTH = $("body").width();
		WIDTH-=10;
		var width;
		width = WIDTH-value
		return width;
	}
	function refreshProTable() {
		$('#pro_table').datagrid('load',{
			planmainid:planmainid,
			startdate:fromDate,
			enddate:toDate
		});
	}
	
	$("#search").click(function(){
		fromDate = $("#fromDate").datebox('getValue');
		toDate = $("#toDate").datebox('getValue');
		refreshProTable();
	});

	function newpro() {
		window.location.href = "../app/plandailyadd.do?planid=" + planmainid;
	}
	function updatepro(index) {
		var item = $("#pro_table").datagrid('getRows')[index];
		window.location.href = "../app/plandailyadd.do?id=" + item.id;
	}

	function deletepro(index) {
		currentPro = $("#pro_table").datagrid('getRows')[index];
		if(currentPro){
			if(currentPro.status==6){
				$.messager.confirm("操作提示", "您确定要执行操作吗？", function (data) {
		            if (data) {
						$.ajax({
							type : 'POST',
							url : "../planDailyRemove.do?id="+currentPro.id,
							contentType : "application/json",
							success : function(data) {
								if (data.success) {
									$.messager.show({
										title : '删除',
										msg : '删除成功',
										timeout : 5000,
										showType : 'slide'
									});
									refreshProTable();
								} else {
									$.messager.alert('删除', '删除失败', 'error');
								}
							},
							dataType : "json"
						});
					}
		            else {
		            }
		        });
			}
			else{
				$.messager.alert('删除', '删除失败', 'error');
			}
		} else {
			$.messager.alert('更新', '请选择要删除的日计划', 'warning');
		}
	}
	
	function savePro() {
		if (!$("#proFm").form('validate'))
			return;
		var content = $("#content").textbox('getValue');
		currentPro.content = content;
		var plandate = $("#plandate").datebox('getValue');
		currentPro.plandate = plandate;
		var percent = $("#percent").textbox('getValue');
		currentPro.percent = percent;
		currentPro.mainid = planmain.id;
		$.ajax({
			type : 'POST',
			url : "../planDailySave.do",
			data : JSON.stringify(currentPro),
			contentType : "application/json",
			success : function(data) {
				if (data.success) {
					$.messager.show({
						title : '保存日计划',
						msg : '保存日计划成功',
						timeout : 5000,
						showType : 'slide'
					});
					refreshProTable();
				} else {
					$.messager.alert('保存日计划', '保存日计划失败', 'error');
				}
				$('#proDlg').dialog('close');
			},
			dataType : "json"
		});
	}
	
	function getRemainPercent(){
		$.ajax({
			type : 'GET',
			url : "../planmain/unfinishpercent?mainid="+planmain.id,
			contentType : "application/json",
			success : function(data) {
				if (data) {
					$("#remainpercent").text(data);
				}
			},
			dataType : "text"
		});
	}
	function formatDate(time) {
		var date = new Date(time);
		var year = date.getFullYear(), month = date.getMonth() + 1, //月份是从0开始的
		day = date.getDate(), hour = date.getHours(), min = date.getMinutes(), sec = date
				.getSeconds();
		var newTime = year + '-' + month + '-' + day;
		return newTime;
	}
</script>
</html>